package cn.zktr.Personnel.model.service.tantiantian;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Optional;

import javax.servlet.http.HttpSession;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.zktr.Personnel.model.dao.tantiantian.TngPlanDao;
import cn.zktr.Personnel.model.mappers.tantiantian.PmsStmMapper;
import cn.zktr.Personnel.model.mappers.tantiantian.TngPlanMapper;
import cn.zktr.Personnel.model.pojos.PerPmsEmp;
import cn.zktr.Personnel.model.pojos.PerSlySet;
import cn.zktr.Personnel.model.pojos.PerTngPlan;
import cn.zktr.Personnel.model.service.zhouyongcong.findService;
import cn.zktr.Personnel.model.service.zhouyongcong.saveAuditService;
import cn.zktr.Personnel.model.service.zhouyongcong.saveService;
import cn.zktr.Personnel.model.service.zhouyongcong.monit.monitObj;
import cn.zktr.Personnel.model.service.zhouyongcong.monit.monitgetService;
import cn.zktr.Personnel.model.service.zhouyongcong.monit.monitsaveService;
import cn.zktr.Personnel.model.uitls.Homapape;

@Service
@Transactional(rollbackOn=Exception.class)
public class TngPlanService {
	@Autowired
	private monitsaveService mss; 
	@Autowired
	private saveAuditService sas;
	/**
	 * 培训计划表
	 */
	@Autowired
	private TngPlanDao planDao;
	@Autowired
	private TngPlanMapper planmer;
	/**
	 * 职员表
	 */
	@Autowired
	private PmsStmMapper stmmer;
	/**
	 * 员工
	 */
	@Autowired
	private findService findser;
	
	/**
	 * @return查询所有培训计划
	 */
	public JSONArray selePlanAll() {
		return planmer.selePlanAll();
	}
	/**
	 * @return查询所有培训部长
	 */
	public JSONArray selePlan(HttpSession session) {
		JSONArray plan = planmer.selePlan();
		if(plan.size()>0) {
			JSONObject o=(JSONObject) plan.get(0);
			if(o.get("EMPID").toString().equals(session.getAttribute("id")+"")) {
				return stmmer.seleStmpotid_zong();
				
			}else {
				return planmer.selePlan();

			}
		}
		return null;
		
	}
	/**
	 * @return新增培训计划
	 */
	public PerTngPlan AddPlan(PerTngPlan entity) {
		return planDao.save(entity);
		
	}/**
	 * @return根据id查询培训计划
	 */
	public Optional<PerTngPlan> selePlanId(Integer id) {
		return planDao.findById(id);
		
	}
	public void tupdate_plan(PerTngPlan plan, String planninds, String planmaxds, Integer shenpiren,
			HttpSession session) {
		// TODO Auto-generated method stub
		//根据计划id查询
		Optional<PerTngPlan> plans = selePlanId(plan.getPlanid());
		//创建人
		
		plan.setPerPmsEmp1(findser.findEmpid(Integer.valueOf(session.getAttribute("id")+"")));
		//审批人
		PerPmsEmp emp = stmmer.seleEmpids(shenpiren);
		plan.setPerPmsEmp2(emp);
		
		//状态
		plan.setPlanstate("0");
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Timestamp ts1 = new Timestamp(format.parse(planninds).getTime());
			plan.setPlannind(ts1);
			Timestamp ts2 = new Timestamp(format.parse(planmaxds).getTime());
			plan.setPlanmaxd(ts2);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		planDao.save(plan);
	}
	@Autowired
	private monitgetService mgs;
	@Autowired
	private findService fs;
	@Autowired
	private saveService ss;
	public monitObj setmo1(Integer planid) {
		PerTngPlan plan = planDao.findById(planid).get();
		mgs.getPojos(plan);
		monitObj mo1 = new monitObj(false,false);
		plan.setPlanstate("1");
		planDao.save(plan);
		mo1.setOld_obj(plan);
		PerTngPlan plan1 = new PerTngPlan();
		JSONArray o=planmer.select_plan_planid(planid);
		if(o.size()>0) {
			plan1.setPlanstate("3");
			mo1.setNew_obj(plan1); 
		}else if(o.size()<=0) {
			plan1.setPlanstate("2");
			mo1.setNew_obj(plan1); 
		}
		return mo1;
	}
	public void plan_songshen(Integer planid,HttpSession session) {
			PerTngPlan plan = planDao.findById(planid).get();
			monitObj[] mos = {setmo1(planid)};

			//1.审核类型
			//2.审核发起人
			//3.审核标题
			//4.审核的文本
			ss.save_Audit_id(7,plan.getPerPmsEmp1().getEmpid(),"培训计划审核", fs.findEmpid(plan.getPerPmsEmp1().getEmpid()).getEmpname()+"发起培训计划审核，请审核！");
			mss.save(plan.getPerPmsEmp1().getEmpid(), mos, 7);
	
	}
	public JSONArray TrainingPlantables_name(String text) {
		return planmer.TrainingPlantables_name("%"+text+"%");
		
	}
}
